#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Windows.Forms;
using WaveTech.GShield.Common;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Factories;
using WaveTech.GShield.Engine.Managers;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Manager.Properties;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Manager
{
	public partial class Login : Form
	{
		private Setting sys;

		#region Constructor

		public Login()
		{
			InitializeComponent();
		}

		#endregion Constructor

		#region Login_Load

		private void Login_Load(object sender, EventArgs e)
		{
			DatabaseManager.Instance.StartService();

			m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
			                                                 m_bannerImage.Height, BannerFactory.BannerStyle.WinVistaBlack,
			                                                 Resources.B48x48_KGPG_Key2, "GShield Login",
			                                                 "");

			sys = SettingsDAO.Get();

			if (sys == null || sys.AdminPassword == null)
			{
				MessageBox.Show("GShield has not been setup, please press the login button to continue.", "GShield Setup",
				                MessageBoxButtons.OK, MessageBoxIcon.Information);
				txtPassword.Enabled = false;
			}
			else
			{
				Focus();
				txtPassword.Focus();
			}
		}

		#endregion Login_Load

		#region btnLogin_Click

		private void btnLogin_Click(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			txtPassword.Enabled = false;

			if (sys == null || sys.AdminPassword == null)
			{
				Cursor.Current = Cursors.Default;

				Setup setup = new Setup();
				setup.Show();

				Hide();
			}
			else
			{
				if (sys != null && sys.AdminEmail == Hash.ComputeHash(txtPassword.Text.Trim(), "SHA256", null))
				{
					Logging.Write("Administration Login Successful.", LogCategory.Security, LogPriority.Low, LogType.Security);

					MDIMain cn = new MDIMain();
					cn.Show();
					Hide();

					Cursor.Current = Cursors.Default;
				}
				else
				{
					Cursor.Current = Cursors.Default;
					txtPassword.Text = "";
					txtPassword.Enabled = true;

					Logging.Write("Invalid Login Attempt.", LogCategory.Security, LogPriority.High, LogType.InvalidLogOn);

					MessageBox.Show("The password supplied is invalid.", "Bad Pasword", MessageBoxButtons.OK, MessageBoxIcon.Error);
					txtPassword.Focus();
				}
			}

			Cursor.Current = Cursors.Default;
			txtPassword.Enabled = true;
		}

		#endregion btnLogin_Click
	}
}